<!--#include file="lib/utf-8.asp"-->
<!--#include file="lib/conn.asp"-->
<!--#include file="lib/json_lastest.asp"-->
<!--#include file="lib/json_util_lastest.asp"-->
<!--#include file="lib/saferequest.asp"-->

<%
Response.ContentType = "application/json"
repeat=0
bStatus="false"
sInfo=""
aData=""
aLog=""
json=""

function CheckVoccRepeat(vocc_id,vocc_name)
	sql_repeat="select count(vocc_id) as num from vocc where vocc_name='"&vocc_name&"'"
	if IsNumeric(vocc_id) and Not IsEmpty(vocc_id) then
		sql_repeat=sql_repeat&" and vocc_id<>"&vocc_id
	end if
	set rs_repeat=Server.CreateObject("ADODB.Recordset")
	rs_repeat.open sql_repeat,conn,1,1
	CheckVoccRepeat=rs_repeat("num")
	rs_repeat.close
	set rs_repeat=nothing
end function

function GenerateCurrentData(sql_cur)
	set rs_cur=server.createobject("adodb.recordset")
	rs_cur.open sql_cur,conn,1,1
	if not rs_cur.eof then
		aData=aData&"{"
		aData=aData&"""vocc_id"":"&rs_cur("vocc_id")&","
		aData=aData&"""vocc_name"":"""&rs_cur("vocc_name")&""","
		aData=aData&"""vocc_chinese"":"""&rs_cur("vocc_chinese")&""","
		aData=aData&"""vocc_english"":"""&rs_cur("vocc_english")&""","
		aData=aData&"""vocc_short"":"""&rs_cur("vocc_short")&""","
		aData=aData&"""vocc_memo"":"""&rs_cur("vocc_memo")&""","
		aData=aData&"""vocc_update"":"""&rs_cur("vocc_update")&""","
		aData=aData&"""vocc_user"":"&rs_cur("vocc_user")
		aData=aData&"}"
	end if
	rs_cur.close
	set rs_cur=nothing
end function



function ExecuteCommand(sql_cmd)
	set cmd=Server.CreateObject("ADODB.Command")
	cmd.ActiveConnection = conn
	cmd.CommandText = sql_cmd
	cmd.Execute
end function


submit=trim(saferequest("submit",0))
vocc_id=trim(saferequest("vocc_id",0))
if submit<>"" then
	'if IsEmpty(vocc_id) or vocc_id="" then vocc_id="null"
	vocc_name=trim(saferequest("vocc",0))
	vocc_chinese=trim(saferequest("chinese",0))
	vocc_english=trim(saferequest("english",0))
	vocc_short=trim(saferequest("short",0))
	vocc_memo=trim(saferequest("memo",0))
	vocc_user=session("MM_ID")
	'repeat=CheckVoccRepeat(vocc_id,vocc_name,vocc_vocc)
	if vocc_name="" then
		sInfo="船公司名空"
	elseif CheckVoccRepeat(vocc_id,vocc_name)>0 then
		sInfo="船公司重复"
	else
		if submit="保存" then
			if not CheckPF(1,15) then
				sInfo="没有编辑权限"
			else
				sql_cmd="update vocc set vocc_name='"&vocc_name&"',vocc_chinese='"&vocc_chinese&"',vocc_english='"&vocc_english&"',vocc_short='"&vocc_short&"',vocc_memo='"&vocc_memo&"',vocc_user="&vocc_user&",vocc_update='"&Now()&"' where vocc_id="&vocc_id
				ExecuteCommand(sql_cmd)
				sql_cur="SELECT * FROM vocc where vocc_user="&session("MM_ID")&" and vocc_id="&vocc_id
				GenerateCurrentData(sql_cur)
				bStatus="true"
			end if
		elseif submit="新增并复制" then
			if not CheckPF(1,15) then
				sInfo="没有添加权限"
			else
				sql_cmd="insert into vocc(vocc_name,vocc_chinese,vocc_english,vocc_short,vocc_memo,vocc_user) values('"&vocc_name&"','"&vocc_chinese&"','"&vocc_english&"','"&vocc_short&"','"&vocc_memo&"',"&vocc_user&")"
				ExecuteCommand(sql_cmd)
				sql_cur="SELECT top 1 * FROM vocc where vocc_user="&session("MM_ID")&" order by vocc_id desc"
				GenerateCurrentData(sql_cur)
				bStatus="true"
			end if
		end if
	end if
elseif submit="" then
	if not CheckPF(1,2) then
		sInfo="没有查看权限"
	else
		if vocc_id<>"" then
			sql_cur="select * from vocc where vocc_id="&vocc_id
			GenerateCurrentData(sql_cur)
		end if
		bStatus="true"
	end if
end if
'response.write sql
'response.end
if bStatus then

	sql_log = "SELECT * FROM vocc where vocc_user="&session("MM_ID")&" and DateValue(vocc_update)=date() order by vocc_update desc"
	set rs_log=server.createobject("adodb.recordset")
	rs_log.open sql_log,conn,1,1
	do while not rs_log.eof
		aLog=aLog&"{"
		aLog=aLog&"""vocc_id"":"&rs_log("vocc_id")&","
		aLog=aLog&"""vocc_name"":"""&rs_log("vocc_name")&""","
		aLog=aLog&"""vocc_chinese"":"""&rs_log("vocc_chinese")&""","
		aLog=aLog&"""vocc_english"":"""&rs_log("vocc_english")&""","
		aLog=aLog&"""vocc_short"":"""&rs_log("vocc_short")&""","
		aLog=aLog&"""vocc_memo"":"""&rs_log("vocc_memo")&""","
		aLog=aLog&"""vocc_update"":"""&rs_log("vocc_update")&""","
		aLog=aLog&"""vocc_user"":"&rs_log("vocc_user")
		aLog=aLog&"},"
		rs_log.movenext
	loop
	rs_log.close
	set rs_log=nothing
	if Len(aLog)>0 then aLog=Left(aLog,Len(aLog)-1)
end if



json="{""bStatus"":"&bStatus
json=json&",""sInfo"":"""&sInfo&""""
json=json&",""aData"":["&aData&"]"
json=json&",""aLog"":["&aLog&"]"
json=json&"}"
response.write(json)
conn.close
set conn=nothing
%>
